<?php
namespace app\customer\controller;

use tp5_lib\account\logic\AccountLogic;
use tp5_lib\user_api\controller\LoginController;
use app\payment\model\PaymentModel;
use tp5_lib\user\model\UserModel;

class ApiController extends LoginController
{
    public function login()
    {
        $data=input();
        if(!$this->validate->scene("login")->check($data)){
            $this->error($this->validate->getError());
        }
        $user=$this->logic->login($data["username"],$data["password"]);
        if(!$user){
            $this->error($this->logic->getError());
        }

        $account=new AccountLogic();
        if(!$account->hasWithdrawWay($user)){
            $this->result($user,-2,"请先绑定提现方式");
        }

        $this->result($user,1);
    }
    /** 消费总笔数，消费总额，可提现额度 */
    public function statistics()
    {
        $user=$this->checkLogin();
        $count=PaymentModel::where(["payer"=>$user["id"],"status"=>"1"])->count();
        $total=PaymentModel::where(["payer"=>$user["id"],"status"=>"1"])->sum("amount");
        $money=UserModel::where(["id"=>$user["id"]])->field("money")->find();
        $this->result([
            "consume_count"=>$count,
            "consume_total"=>$total,
            "money"=>$money["money"],
        ],1);
    }
}
